package com.tuju.jetpackfirstdemo.utils;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;

import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.ChartTouchListener;
import com.github.mikephil.charting.listener.OnChartGestureListener;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils;
import com.tuju.jetpackfirstdemo.R;
import com.tuju.jetpackfirstdemo.ui.mpchart.KLineBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class KLineHelper {
    //-===========辅助方法================
    public static List<Float> getMaxMinY(List<Float> yData, List<KLineBean.PointBean> pointData) {
        if (yData == null) {
            return Arrays.asList(0f, 100f);
        }
        float minValueY = yData.get(0);
        float maxValueY = 0;

        //确定y轴最大值与最小值
        for (int i = 0; i < yData.size(); i++) {
            maxValueY = Math.max(maxValueY, yData.get(i));
            minValueY = Math.min(minValueY, yData.get(i));
        }

        if (pointData != null) {
            //查看一下点坐标 如果有更大的值则替换成坐标最大值
            for (int i = 0; i < pointData.size(); i++) {
                maxValueY = Math.max(maxValueY, pointData.get(i).getY());
                minValueY = Math.min(minValueY, pointData.get(i).getY());
            }
        }
        Log.i("yanze", "Y: " + " max: " + maxValueY + " min：" + minValueY);
        return Arrays.asList(minValueY, maxValueY);
    }

    public static List<Long> getMaxMinX(List<String> xTime, List<KLineBean.PointBean> pointData) {
        if (xTime == null) {
            return Arrays.asList(0l, 0l);
        }
        long minValueX = dateToStamp(xTime.get(0));
        long maxValueX = 0;

        //确定x轴最大值与最小值
        for (int i = 0; i < xTime.size(); i++) {
            long timeValue = dateToStamp(xTime.get(i));
            maxValueX = Math.max(maxValueX, timeValue);
            minValueX = Math.min(minValueX, timeValue);
        }

        if (pointData != null) {
            //查看一下点坐标 如果有更大的值则替换成坐标最大值
            for (int i = 0; i < pointData.size(); i++) {
                String timeStr = pointData.get(i).getX();
                maxValueX = Math.max(maxValueX, dateToStamp(timeStr));
                minValueX = Math.min(minValueX, dateToStamp(timeStr));
            }
        }
        Log.i("yanze", "X: " + " max: " + maxValueX + "(" + stampToDate(maxValueX) + ")" + " min：" + minValueX + "(" + stampToDate(minValueX) + ")");
        return Arrays.asList(minValueX, maxValueX);
    }

    /*
     * 将时间转换为时间戳
     */
    public static long dateToStamp(String oldS) {
        String s = oldS.replace("T", " ");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            return 0;
        }
        long ts = date.getTime();
        return ts;
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }

    //格式化一下sb时间
    public static String formatSbTime(String time) {
        String[] times = time.split(" ");
        String year = times[0].substring(5, times[0].length());
        String hour = times[1].substring(0, 5);
        return year + " " + hour;
    }

    //自定义X轴显示
    public static class MyXFormattrt extends ValueFormatter {

        @Override
        public String getFormattedValue(float value) {
            Float fvalue = value;
            String time = stampToDate(fvalue.longValue());
            return formatSbTime(time);
        }
    }

    //自定义X轴显示
    public static class MyYFormattrt extends ValueFormatter {

        @Override
        public String getFormattedValue(float value) {
            return value + "℃";
        }
    }

    /**
     * 随机颜色
     */
    public static int randomColor() {
        Random random = new Random();
        int red = random.nextInt(150) + 50;//50-199
        int green = random.nextInt(150) + 50;//50-199
        int blue = random.nextInt(150) + 50;//50-199
        return Color.rgb(red, green, blue);
    }

    /**
     * 自定义MyMarkerView
     */
    public static class MyMarkerView extends MarkerView {
        private TextView tv_y;
        private TextView tv_x;

        public MyMarkerView(Context context, int layoutResource) {
            super(context, layoutResource);
            tv_y = (TextView) findViewById(R.id.tv_y);
            tv_x = (TextView) findViewById(R.id.tv_x);
        }

        @Override
        public void refreshContent(Entry e, Highlight highlight) {
//            if (e instanceof CandleEntry) {
//                CandleEntry ce = (CandleEntry) e;
//                tvContent.setText("" + Utils.formatNumber(ce.getHigh(), 0, true));
//            } else {
//                tvContent.setText("" + Utils.formatNumber(e.getY(), 0, true));
//            }
            float y = e.getY();
            long xValue = (long) e.getX();
            String xStr = stampToDate(xValue);
            tv_y.setText(y + "℃");
            tv_x.setText(xStr);
            super.refreshContent(e, highlight);
        }

        @Override
        public MPPointF getOffset() {
            return new MPPointF(-(getWidth() / 2), -getHeight());
        }
    }

    /**
     * 双击事件监听
     */
    public static abstract class CoupleChartGestureListener implements OnChartGestureListener {
        @Override
        public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
        }

        @Override
        public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {

        }

        @Override
        public void onChartLongPressed(MotionEvent me) {

        }

        @Override
        public void onChartDoubleTapped(MotionEvent me) {
        }

        @Override
        public void onChartSingleTapped(MotionEvent me) {

        }

        @Override
        public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {

        }

        @Override
        public void onChartScale(MotionEvent me, float scaleX, float scaleY) {

        }

        @Override
        public void onChartTranslate(MotionEvent me, float dX, float dY) {

        }
    }

}
